package com.macro.mall.product.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.macro.mall.mapper.PmPrptyMapper;
import com.macro.mall.mapper.PmPrptyPackageMapper;
import com.macro.mall.mapper.PmPrptyValMapper;
import com.macro.mall.model.*;
import com.macro.mall.product.constant.Constants;
import com.macro.mall.product.dto.PmPrptyDto;
import com.macro.mall.product.dto.PmPrptyPackageDto;
import com.macro.mall.product.dto.PmPrptyValDto;
import com.macro.mall.product.service.PmPrptyPackageService;
import com.macro.mall.product.service.PmPrptyService;
import com.macro.mall.product.util.DozerUtils;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.dozer.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class PmPrptyPackageServiceImpl implements PmPrptyPackageService {
    @Autowired
    private PmPrptyPackageMapper pmPrptyPackageMapper;
    @Autowired
    private PmPrptyMapper pmPrptyMapper;
    @Autowired
    private PmPrptyValMapper pmPrptyValMapper;
    @Autowired
    private Mapper mapper;

    @Autowired
    private PmPrptyService pmPrptyService;

    @Override
    public int save(PmPrptyPackageDto pmPrptyPackageDto) {
        int count =0;
        if(pmPrptyPackageDto!=null && !StringUtils.isBlank(pmPrptyPackageDto.getPrptyIds())){
            PmPrptyPackage prptyPackage= mapper.map(pmPrptyPackageDto,PmPrptyPackage.class);
            if(prptyPackage.getId()!=null && prptyPackage.getId()>0){
                count= pmPrptyPackageMapper.updateByPrimaryKey(prptyPackage);
            }else{
                prptyPackage.setStatus(Constants.STATUS_VALID);
                count= pmPrptyPackageMapper.insert(prptyPackage);
            }
        }
        return count;
    }

    @Override
    public List<PmPrptyPackage> listPage(int pageNum, int pageSize, PmPrptyPackageDto param) {
        PageHelper.startPage(pageNum, pageSize);
        PmPrptyPackageExample example  = new PmPrptyPackageExample();
        PmPrptyPackageExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(Constants.STATUS_VALID);
        if(!StringUtils.isBlank(param.getName())){
            criteria.andNameLike("%"+param.getName()+"%");
        }
        example.setOrderByClause(" name asc ");
        List<PmPrptyPackage> packageList = pmPrptyPackageMapper.selectByExample(example);


        if(packageList!=null && packageList.size()>0){
            for(PmPrptyPackage prptyPackage : packageList){
               this.getPackageInfo(prptyPackage);
            }
        }
        return packageList;
    }
    private  PmPrptyPackage getPackageInfo(PmPrptyPackage prptyPackage){
        if(prptyPackage!=null && !StringUtils.isBlank(prptyPackage.getPrptyIds())){
            String[] prptyIds = prptyPackage.getPrptyIds().split(";");
            PmPrpty  prpty;
            List<PmPrpty> prptyList = new ArrayList<>();
            for(String prptyId : prptyIds){
                 prpty = pmPrptyService.getById(Long.valueOf(prptyId));
                 if(prpty!=null && Constants.STATUS_VALID==prpty.getStatus().intValue()){
                     prptyList.add(prpty);
                 }
            }
            prptyPackage.setPrptyList(prptyList);
        }
        return  prptyPackage;
    }
    @Override
    public int deletePackage(String packageIds){
        int count =0;
        if(!StringUtils.isBlank(packageIds)){
            String[] packageIdArray =packageIds.split(";");
            for(String packageId : packageIdArray){
                int countInfo=pmPrptyPackageMapper.deleteByPrimaryKey(Long.valueOf(packageId));
                count= count+countInfo;
            }
        }
        return count;
    }
    @Override
    public PmPrptyPackage getById(Long id){
        PmPrptyPackage prptyPackage = pmPrptyPackageMapper.selectByPrimaryKey(id);
        this.getPackageInfo(prptyPackage);
        return  prptyPackage;
    }
}
